package com.carepad.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.carepad.dao.RecordsMapper;
import com.carepad.entity.Records;
import com.carepad.service.RecordAnswerService;

@Service("recordAnswerService")
public class RecordAnswerServiceImpl extends AbstractService implements
		RecordAnswerService {

	@Autowired
	private RecordsMapper recordsMapper;

	@Override
	public List<Records> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Records> findAllNotDisabled() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Records> findAllDisabled() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Records findById(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int save(Records entity) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(Records entity) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delete(Records entity) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delete(Integer id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int save(Records entity, Integer systemUserID) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(Records entity, Integer systemUserID) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int saveMultiple(List<Records> recordList, String systemUserName) {

		int ret = 0;
		try {
			for (int i = 0; i < recordList.size(); i++) {
				Date d1 = recordList.get(i).getExecutionDate();
				SimpleDateFormat df = new SimpleDateFormat("MM/dd/YYYY");
				String formattedDate = df.format(d1);
				
				List<Records> existingRecords = recordsMapper
						.findExistingRecord(recordList.get(i).getSessionId(),
								recordList.get(i).getHcsId(), recordList.get(i)
										.getActionItem(), formattedDate);
				recordList.get(i).setStaffName(systemUserName);
				recordList.get(i).setRefuser(systemUserName);
				if (existingRecords != null && existingRecords.size() > 0) {
					saveEditSettings(recordList.get(i));
					recordList.get(i).setUid(existingRecords.get(0).getUid());
					ret += recordsMapper.updateByPrimaryKeySelective(recordList
							.get(i));
				} else {
					saveAddSettings(recordList.get(i));
					ret += recordsMapper.insertWithoutId(recordList.get(i));
				}
				// Calendar calendar = Calendar.getInstance();
				// recordList.get(i).setExecutionDate(calendar.getTime());
				// recordList.get(i).setActionTimeFrom(actionTimeFrom)

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
}
